package com.youmu.sichuansfc.dao.impl;

import com.youmu.sichuansfc.dao.NotifyReadDao;
import com.youmu.sichuansfc.domain.YoumuNotifyReadHistory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

/**
 * @author WYZ
 * @create 2018-02-01 10:41
 * @desc
 **/
@Repository
public class NotifyReadDaoImpl implements NotifyReadDao{

    private EntityManager em;
    @PersistenceContext
    public void setEm(EntityManager em) {
        this.em = em;
    }

    @Override
    public YoumuNotifyReadHistory insert(YoumuNotifyReadHistory youmuNotifyReadHistory) {
        //先查询 是否已经阅读
        if (findBy(youmuNotifyReadHistory.getNotifyId(), youmuNotifyReadHistory.getFundUserId()) != null){

        }else {
            em.persist(youmuNotifyReadHistory);
        }
        return youmuNotifyReadHistory;
    }

    public YoumuNotifyReadHistory findBy(Long nofityId, Long fundUserId){
        String sql = "from YoumuNotifyReadHistory where isDelete = 0 and notifyId =:notifyId and fundUserId =:fundUserId ";
        List<YoumuNotifyReadHistory> youmuNotifyReadHistoryList = em.createQuery(sql, YoumuNotifyReadHistory.class)
                .setParameter("notifyId", nofityId)
                .setParameter("fundUserId", fundUserId)
                .getResultList();
        if (youmuNotifyReadHistoryList != null && youmuNotifyReadHistoryList.size() > 0)
            return  youmuNotifyReadHistoryList.get(0);
        return null;
    }
}
